package updown.controller.other;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import updown.constant.LogConstant;
import updown.domain.Account;
import updown.domain.Log;
import updown.domain.ResultVo;
import updown.service.task.LogService;
import updown.service.task.LoginService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>Title: ww.kaixin.downupservice.controller </p>
 * <p>Description:updown: - 登录controller </p>
 *
 * @Author:jiangkaixin
 * @Date:2021/11/5
 * @Version: 1.0
 */
@Controller
@Slf4j
public class LoginController extends BaseController {

    @Autowired
    private LoginService superLoginService;

    @Autowired
    private LogService logService;

    @GetMapping("/home")
    public String login() {
        return "login";
    }

    /**
     * 登录
     * @param request
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    public ResultVo login(@RequestBody Account account, HttpServletRequest request) {
        Account retAccount = superLoginService.login(account);
        addLog(retAccount.getUsername(),"登录", LogConstant.MODULE_LOGIN);
        return writeToSuccess(retAccount);
    }

    /**
     * 获取账号列表
     * @param request
     * @return
     */
    @GetMapping("/getAccountList")
    public String getAccountList(Map<String, Object> model) {
        List<Account> accounts = superLoginService.getAccountList(null);
        accounts.stream().forEach(item->{
            item.setPassword(null);
        });
        model.put("accounts",accounts);
        List<Log> logs = logService.getLogList(new Log());
        model.put("logs",logs);
        return "accountAndLog";
    }
}
